package com.ruoyi.system.rocketmq.consumer;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.domain.constant.enums.MessTopicAndTag;
import com.ruoyi.common.domain.constant.to.MessageActivityTo;
import com.ruoyi.system.rocketmq.netty.NettyWebSocketServer;
import com.ruoyi.system.service.IActivityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
@Slf4j
@RocketMQMessageListener(topic = "yl_comment", consumerGroup = "yl-consumer-comment-group", selectorExpression = "*"  // 控制tag消费 在后面拼接  消费所有Tag的消息
)
public class YlCommentConsumer implements RocketMQListener<MessageExt> {


    @Resource
    private IActivityService activityService;

    @Resource
    private NettyWebSocketServer nettyWebSocketServer;


    @Override
    public void onMessage(MessageExt messageExt) {
        MessageActivityTo to = JSON.parseObject(messageExt.getBody(), MessageActivityTo.class);

        if (MessTopicAndTag.YL_COMMENT_NOTICE_AUDITORS.getTag().equals(messageExt.getTags())) {
            try {
                //用户评论 通知客服查询新信息
                nettyWebSocketServer.sendToUser(String.valueOf(to.getSysUserId()), "{\n" +
                        "  \"title\": \"审批提醒\",\n" +
                        "  \"content\": \"您有新的待办审批单据 #1234\",\n" +
                        "  \"type\": \"warning\"\n" +
                        "}\n");

            } catch (Exception e) {
                log.error("消费消息失败，将自动重试", e);
                throw new RuntimeException("消费失败", e);
            }

        }

    }
}
